package com.meizu.asum.richtextsdk.uiframework.xlayoutparam.utils.hiden;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.meizu.asum.richtextsdk.uiframework.xlayoutparam.utils.DensityUtils;

public class XViewGrpLayoutParams extends XBaseLayoutParams {
    private ViewGroup.LayoutParams layoutParams;

    public XViewGrpLayoutParams(Context context, View view) {
        super(context, view);

        layoutParams = view.getLayoutParams();
        if (layoutParams == null) {
            layoutParams = new ViewGroup.LayoutParams(0, 0);
        }
        measureView();
    }

    public void setDp(int x, int y, int w, int h) {
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;

        analyseXywhFromType(0);
        setXywh();
    }

    public void setDiv(int x, int y, int w, int h) {
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;

        analyseXywhFromType(1);
        setXywh();
    }

    public void setPx(int x, int y, int w, int h) {
        this.x = x;
        this.y = y;
        this.w = w;
        this.h = h;

        analyseXywhFromType(2);
        setXywh();
    }

    protected void analyseXywhFromType(int type) {
        analyseW(type);
        analyseH(type);
        analyseX(type);
        analyseY(type);
    }

    protected void analyseW(int type) {
        if (w == XBaseArea.MATCH) {
            w = ViewGroup.LayoutParams.MATCH_PARENT;
        } else if (w == XBaseArea.INVARIANT) {
            if (view.getWidth() == 0) {
                w = view.getMeasuredWidth();
            } else {
                w = view.getWidth();
            }
        } else if (w == XBaseArea.WRAP) {
            w = ViewGroup.LayoutParams.WRAP_CONTENT;
        } else {
            if (type == 0) {
                w = DensityUtils.dip2px(context, w);
            } else if (type == 1) {
                w = DensityUtils.div2px(context, w, DensityUtils.Type.WIDTH);
            }
        }
    }

    protected void analyseH(int type) {
        if (h == XBaseArea.MATCH) {
            h = ViewGroup.LayoutParams.MATCH_PARENT;
        } else if (h == XBaseArea.INVARIANT) {
            if (view.getHeight() == 0) {
                h = view.getMeasuredHeight();
            } else {
                h = view.getHeight();
            }
        } else if (h == XBaseArea.WRAP) {
            h = ViewGroup.LayoutParams.WRAP_CONTENT;
        } else {
            if (type == 0) {
                h = DensityUtils.dip2px(context, h);
            } else if (type == 1) {
                h = DensityUtils.div2px(context, h, DensityUtils.Type.HEIGHT);
            }
        }
    }

    protected void analyseX(int type) {
        if (x == XBaseArea.CENTER || x == XBaseArea.INVARIANT || x == XBaseArea.LEFT || x == XBaseArea.RIGHT) {
            x = 0;
            Log.e("XJW", "ViewGroup控件不支持设置关系型X坐标，只能使用具体数值");
        } else {
            if (type == 0) {
                x = DensityUtils.dip2px(context, x);
            } else if (type == 1) {
                x = DensityUtils.div2px(context, x, DensityUtils.Type.WIDTH);
            }
        }
    }

    protected void analyseY(int type) {
        if (y == XBaseArea.CENTER || y == XBaseArea.INVARIANT || y == XBaseArea.TOP || y == XBaseArea.BOTTOM) {
            y = 0;
            Log.e("XJW", "ViewGroup控件不支持设置关系型Y坐标，只能使用具体数值");
        } else {
            if (type == 0) {
                y = DensityUtils.dip2px(context, y);
            } else if (type == 1) {
                y = DensityUtils.div2px(context, y, DensityUtils.Type.HEIGHT);
            }
        }
    }

    protected void setXywh() {
        if (XBaseArea.IS_PRINT) {
            Log.i("XJW", "ViewGrp：X：" + x + "，Y：" + y + "，W：" + w + "，H：" + h);
        }
        layoutParams.height = h;
        layoutParams.width = w;
        view.setLayoutParams(layoutParams);
    }
}
